package main

import (
	"fmt"
	"strconv"
	"strings"
)

/* 412. Fizz Buzz  https://leetcode.cn/problems/fizz-buzz/ */

func ExecQ412() {
	fmt.Println(fizzBuzz(3))
	fmt.Println(fizzBuzz(5))
	fmt.Println(fizzBuzz(15))
}

// 我写的  👆 内存小
func fizzBuzz1(n int) []string {
	word1, word2 := "Fizz", "Buzz"
	rst := make([]string, n)
	for i := 1; i <= n; i++ {
		if i%3 == 0 && i%5 == 0 {
			rst[i-1] = word1 + word2
		} else if i%3 == 0 {
			rst[i-1] = word1
		} else if i%5 == 0 {
			rst[i-1] = word2
		} else {
			rst[i-1] = strconv.Itoa(i)
		}
	}
	return rst
}

// 官方的  👇 内存大
func fizzBuzz(n int) (rst []string) {

	for i := 1; i <= n; i++ {
		sb := &strings.Builder{}
		if i%3 == 0 {
			sb.WriteString("Fizz")
		}
		if i%5 == 0 {
			sb.WriteString("Buzz")
		}
		if sb.Len() == 0 {
			sb.WriteString(strconv.Itoa(i))
		}
		rst = append(rst, sb.String())
	}
	return
}
